package edu.webtek.graven;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

public class SessionData{
	private boolean loggedIn     = false;
	private int     userID 	     = - 1;
	private String  userName     = "";
	private String  message		 = "";
	final private HttpSession session;
	public SessionData(HttpSession session) {
		this.session = session;
		Enumeration<String> names = session.getAttributeNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			String data = (String) session.getAttribute(name);
			switch(name){
			case "userName":
				userName = data;
				break;
			case "userID":
				userID = Integer.parseInt(data);
				break;
			case "loggedIn":
				loggedIn = Boolean.parseBoolean(data);
				break;
			}
		}
	}
	public void saveData(){
		session.setAttribute("userID"  , Integer.toString(userID));
		session.setAttribute("userName", userName);
		session.setAttribute("loggedIn", Boolean.toString(loggedIn));
	}
	public int getUserID() {
		return userID;
	}
	public void setCustomerID(int userID) {
		this.userID = userID;
	}
	public String getUserName() {
		return userName;
	}
	public void setCustomerName(String userName) {
		this.userName = userName;
	}
	public boolean isLoggedIn() {
		return loggedIn;
	}
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		if(message == null) message = "";
		this.message = message;
	}
	public void logout(){
		setCustomerID(-1);
		setCustomerName("");
		setLoggedIn(false);
	}
	@Override
	public String toString() {
		return "SessionData [loggedIn=" + loggedIn + ", userID=" + userID
				+ ", userName=" + userName + ", message=" + message
				+ ", session=" + session + "]";
	}
	
	
}